package cn.achao.csr.chat.dao;

import cn.achao.csr.api.model.entity.ChatRoomFriend;
import cn.achao.csr.api.model.entity.ChatRoomGroup;
import cn.achao.csr.chat.mapper.ChatRoomFriendMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class ChatRoomFriendDao extends ServiceImpl<ChatRoomFriendMapper, ChatRoomFriend> {

    public ChatRoomFriend getByRoomId(Long roomId) {
        return lambdaQuery()
                .eq(ChatRoomFriend::getRoomId, roomId)
                .one();
    }

    public Map<Long, ChatRoomFriend> getBatchByRoomIds(List<Long> roomIds) {
        if (roomIds == null || roomIds.isEmpty()) {
            return new HashMap<>();
        }

        return lambdaQuery()
                .in(ChatRoomFriend::getRoomId, roomIds)
                // 添加列表查询方法，并通过Stream转换为Map
                .list()
                .stream()
                .collect(Collectors.toMap(
                        ChatRoomFriend::getId,
                        Function.identity(),
                        (existing, replacement) -> existing  // 处理重复键（根据需求选择处理策略）
                ));
    }
}

